<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Tcourse extends Base{
    //课程搜索
    public function coursesearch(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.key"))){
            $key=input('param.key');
        }else{
            return json(['code' => 503, 'msg' => '搜索关键词呢']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $products=Db::name('course')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('id desc')->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','key'=>$key,'data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $products=Db::name('course')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('id desc')->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','key'=>$key,'data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //PC课程
    public function courses(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $courses=Db::name('course')->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->select();
            if($courses){
                return json(['code' => 200, 'msg' => '成功！','data'=>$courses]);exit();
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    public function pcourses(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $courses=Db::name('course')->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->select();
                if($courses){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$courses]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $courses=Db::name('course')->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->select();
                if($courses){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$courses]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //推荐课程
    public function upcourses(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $courses=Db::name('course')->where('flag',1)->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->select();
            if($courses){
                for ($i=0; $i <count($courses) ; $i++) {
                    $courses[$i]['times']=date('Y-m-d',$courses[$i]['time']);
                    $courses[$i]['year']=date('Y',$courses[$i]['time']);
                    $courses[$i]['monDay']=date('m-d',$courses[$i]['time']);
                }
                return json(['code' => 200, 'msg' => '成功！','data'=>$courses]);exit();
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //课程详情
    public function coursedetail(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '详情id呢']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $detail=Db::name('course')->alias('a')->join('teacher b', 'a.teacher_id = b.id','LEFT')->field('a.*,b.teacher,b.photo,b.age,b.zodiac')->where('a.cus_id',$cus_id)->where('a.state',1)->where('a.id',$id)->find();
            if($detail){
                $detail['times']=date('Y-m-d',$detail['time']);
                return json(['code' => 200, 'msg' => '成功！','data'=>$detail]);exit();
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //PC导师团队
    public function teacherteam(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $teachers=Db::name('teacher')->where('flag',1)->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->select();
            if($teachers){
                return json(['code' => 200, 'msg' => '成功！','data'=>$teachers]);exit();
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
}